# -*- coding: utf-8 -*-

"""
Date: 2023/2/27 09:14
Author: Darker
"""
import enum
import re
from typing import List, Dict, Text

from utils.model import mySqlite


class BugStatus(enum.Enum):
    NEW = "新"
    REJECTED = "已拒绝"
    RESOLVED = "已解决"
    REOPENED = "重新打开"
    VERIFIED = "已验证"
    ASSIGNED = "确认产品bug"
    CLOSED = "已关闭"

    @classmethod
    def __names(cls):
        return [obj.name for obj in cls]

    @classmethod
    def new_bug_status_obj(cls, name: Text):
        nameUpper = name.upper() if name else "" or "-空-"
        if nameUpper not in cls.__names():
            nameUpper = "CLOSED"
        return eval(f"cls.{nameUpper}")


class BugSeverity(enum.Enum):
    FATAL = "致命"
    SERIOUS = "严重"
    NORMAL = "一般"
    PROMPT = "提示"
    ADVICE = "建议"

    @classmethod
    def __names(cls):
        return [obj.name for obj in cls]

    @classmethod
    def new_bug_severity_obj(cls, name: Text):
        nameUpper = name.upper() if name else "" or "-空-"
        if nameUpper not in cls.__names():
            nameUpper = "NORMAL"
        return eval(f"cls.{nameUpper}")


class BugPriority(enum.Enum):
    URGENT = "紧急"
    HIGH = "高"
    MEDIUM = "中"
    LOW = "低"
    INSIGNIFICANT = "无关紧要"

    @classmethod
    def __names(cls):
        return [obj.name for obj in cls]

    @classmethod
    def new_bug_priority_obj(cls, name: Text):
        nameUpper = name.upper() if name else "" or "-空-"
        if nameUpper not in cls.__names():
            return cls(names=name, value="-空-")
        return eval(f"cls.{nameUpper}")


def trans_bug_status_name_to_value(bugStatus: Text):
    """缺陷状态"""
    return BugStatus.new_bug_status_obj(bugStatus).value


def trans_bug_severity_name_to_value(bugSeverity: Text):
    """缺陷严重程度"""
    return BugSeverity.new_bug_severity_obj(bugSeverity).value


def trans_bug_priority_name_to_value(bugPriority: Text):
    """缺陷严重程度"""
    return BugPriority.new_bug_priority_obj(bugPriority).value


def trans_normal_fields(bugObj, field: Text):
    """created、resolved、closed、reject_time、reopen_time转换"""
    return re.sub(r"(\d.*?) .{8}", r"\1", bugObj.get(field).replace("--", ""))


def trans_title_field(title: Text):
    """转换title字段"""
    if isinstance(title, Text):
        return title.replace("'", "\\").replace("/", "\\/").replace("%","\\%").replace("[", "\\[").replace("]", "\\]")
    else:
        return ""


def trans_common_field(fieldName: Text):
    """reporter、de、closer、fixer、current_owner转换"""
    if isinstance(fieldName, Text):
        return fieldName.replace(";", "").replace("--", "")
    else:
        return ""


def data_cleanup(tapdData: List) -> List:
    """
    清洗tapd缺陷数据，组装成insert语句
    :param tapdData: tapd数据
    :return: list[insert语句]
    """
    sqlList = []
    for bug in tapdData:
        bug["id"] = bug.get("short_id")
        bug["status"] = trans_bug_status_name_to_value(bug.get("status"))
        bug["severity"] = trans_bug_severity_name_to_value(bug.get("severity"))
        bug["priority"] = trans_bug_priority_name_to_value(bug.get("priority"))
        bug["created"] = trans_normal_fields(bug, "created")
        bug["resolved"] = trans_normal_fields(bug, "resolved")
        bug["closed"] = trans_normal_fields(bug, "closed")
        bug["reject_time"] = trans_normal_fields(bug, "reject_time")
        bug["reopen_time"] = trans_normal_fields(bug, "reopen_time")
        bug["title"] = trans_title_field(bug.get("title"))
        bug["custom_field_for"] = bug.get("custom_field_for") or "0"
        bug["reporter"] = trans_common_field(bug.get("reporter"))
        bug["de"] = trans_common_field(bug.get("de")) or bug["reporter"]
        bug["closer"] = trans_common_field(bug.get("closer"))
        bug["fixer"] = trans_common_field(bug.get("fixer"))
        bug["current_owner"] = trans_common_field(bug.get("current_owner"))

        sqlText = f"""INSERT INTO SCORES VALUES('{bug["id"]}', '{bug["title"]}', '{bug["severity"]}', 
        '{bug["status"]}', '{bug["current_owner"]}', '{bug["reporter"]}', '{bug["created"]}', '{bug["fixer"]}', 
        '{bug["resolved"]}', '{bug["closed"]}', '{bug["reject_time"]}', '{bug["reopen_time"]}', '{bug["de"]}', 
        '{bug["closer"]}', '{bug["priority"]}', '{bug["custom_field_four"]}')"""
        sqlList.append(sqlText)
    return sqlList


if __name__ == '__main__':
    trans_bug_status_name_to_value("closed1")
